package com.appbee.gdata.picasaadmin.client.activity;

import javax.inject.Inject;

import com.appbee.core.client.msg.ui.IMessageView;
import com.appbee.gdata.picasaadmin.shared.PicasaSettingsRequestFactory;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;

public class PicasaSettingsActivity extends AbstractActivity
{

	public interface View extends IsWidget
	{

		void setUserName(String response);

		String getPassword();

		String getUserName();

		IMessageView getMessageView();

		void setActivity(PicasaSettingsActivity picasaSettingsActivity);

	}

	private final View view;
	private final PicasaSettingsRequestFactory requestFactory;

	@Inject
	public PicasaSettingsActivity(View view,
									PicasaSettingsRequestFactory requestFactory)
	{
		this.view = view;
		this.requestFactory = requestFactory;
		this.view.setActivity(this);

	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus)
	{
		requestFactory.getPicasaSettingsService().getUserName()
				.fire(new Receiver<String>()
		{

			@Override
			public void onSuccess(String response)
			{
				view.setUserName(response);
			}

		});
		panel.setWidget(view);
	}

	public void onSave()
	{
		requestFactory.getPicasaSettingsService()
				.saveUserInfo(view.getUserName(), view.getPassword())
				.fire(new Receiver<Void>()
				{

					@Override
					public void onSuccess(Void response)
					{
						view.getMessageView().showInfo(
SafeHtmlUtils.fromTrustedString("Vistað"));
					}

				});
	}

	public void onFlushCache()
	{
		requestFactory.getPicasaSettingsService().flushCache()
				.fire(new Receiver<Void>()
		{

			@Override
			public void onSuccess(Void response)
			{
				view.getMessageView().showInfo(
SafeHtmlUtils.fromTrustedString("Chache flussað"));
			}

		});
	}

}
